package cn.skyquant.quant4j.api.forex;

import cn.skyquant.quant4j.api.dto.ResultDTO;
import cn.skyquant.quant4j.api.enums.BorkerType;
import cn.skyquant.quant4j.api.enums.ForexConfigType;
import cn.skyquant.quant4j.api.service.BaseServiceProxy;
import cn.skyquant.quant4j.sdk.enums.TradeDirection;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ForexServiceProxy extends BaseServiceProxy implements ForexService{
    private static final String series_url = default_urlfix+"/forex/config/series";
    private static final String symbol_url = default_urlfix+"/forex/config/symbol";

    public ForexServiceProxy(String accountCode, BorkerType borkerType) {
        super(accountCode, borkerType);
    }


    @Override
    public ResultDTO<List<ForexConfigDTO>> findConfigByTypeAndBorkerType(ForexConfigType type, BorkerType borkerType) {
        return error;
    }

    @Override
    public ResultDTO<ForexConfigDTO> getConfigById(Integer id) {
        return error;
    }

    @Override
    public ResultDTO<Void> updateConfig(Integer id, String name) {
        return error;
    }

    @Override
    public ResultDTO<Void> addConfig(String name, BorkerType borkerType, ForexConfigType type) {
        return error;
    }

    @Override
    public ResultDTO<Void> deleteConfig(Integer id) {
        return error;
    }

    @Override
    public ResultDTO<Void> copyConfig(Integer id, String newName) {
        return error;
    }

    @Override
    public ResultDTO<Void> updateConfigSymbol(Integer configId, List<ForexConfigSymbolDTO> list) {
        return error;
    }

    public ResultDTO<List<ForexConfigSymbolDTO>> findConfigSymbol(Integer configId) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("configId",configId.toString());
        JSONObject jsonObject = get(symbol_url,paramMap);
        if(jsonObject!=null){
            boolean ok = jsonObject.getBoolean("ok");
            String message = jsonObject.getString("message");
            if(ok){
                JSONArray data = jsonObject.getJSONArray("data");
                List<ForexConfigSymbolDTO> list = convertJsonToSymbol(data);
                return ResultDTO.success(list);
            }else {
                return ResultDTO.error(message);
            }
        }
        return error;
    }

    public ResultDTO<Void> updateConfigSeries(Integer configId, List<ForexConfigSeriesDTO> list) {
        return ResultDTO.error;
    }

    public ResultDTO<List<ForexConfigSeriesDTO>> findConfigSeries(Integer configId) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("configId",configId.toString());
        JSONObject jsonObject = get(series_url,paramMap);
        if(jsonObject!=null){
            boolean ok = jsonObject.getBoolean("ok");
            String message = jsonObject.getString("message");
            if(ok){
                JSONArray data = jsonObject.getJSONArray("data");
                List<ForexConfigSeriesDTO> list = convertJsonToSeries(data);
                return ResultDTO.success(list);
            }else {
                return ResultDTO.error(message);
            }
        }
        return error;
    }

    private static final List<ForexConfigSeriesDTO> convertJsonToSeries(JSONArray jsonArray) {
        List<ForexConfigSeriesDTO> list = new ArrayList<>();
        for(int i=0;i<jsonArray.size();i++){
            ForexConfigSeriesDTO dto = new ForexConfigSeriesDTO();
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            dto.i = jsonObject.getInteger("i");
            dto.addpip = jsonObject.getInteger("addpip");
            dto.backpip = jsonObject.getInteger("backpip");
            dto.prs = jsonObject.getInteger("prs");
            dto.sl = jsonObject.getInteger("sl");
            dto.tp = jsonObject.getInteger("tp");
            dto.time = jsonObject.getInteger("time");
            list.add(dto);
        }
        return list;
    }

    private static final List<ForexConfigSymbolDTO> convertJsonToSymbol(JSONArray jsonArray) {
        List<ForexConfigSymbolDTO> list = new ArrayList<>();
        for(int i=0;i<jsonArray.size();i++){
            ForexConfigSymbolDTO dto = new ForexConfigSymbolDTO();
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            dto.i = jsonObject.getInteger("i");
            dto.symbol = jsonObject.getString("symbol");
            dto.tradeDirection = TradeDirection.parse(jsonObject.getString("tradeDirection"));
            dto.standardPosition = jsonObject.getInteger("standardPosition");
            list.add(dto);
        }
        return list;
    }
}
